import { _checkType } from './check-type.js';


function _ERROR(x) {
	const prompt = 'The arugment be sure to greater or equals to 0';
	if (x < 0) {
		throw new Error(prompt);
	}
}


function factorial(x) {
	_checkType(x, 'number');
	_ERROR(x);

	let result = 1;

	for (let i = 1; i <= x; i++) {
		result *= i;
	}

	return result;
}


function unsafeFactorial(x) {
	_checkType(x, 'number');
	_ERROR(x);

	if (x === 0) {
		return 1;
	}

	return x * unsafeFactorial(x - 1);
}



function compare(x, y) {
	_checkType(x, 'number');
	_checkType(y, 'number');

	if (x > y) {
		return 1;
	} else if (x < y) {
		return -1;
	}

	return 0;
}


function isPythaTheorem(a, b, c) {
	_checkType(a, 'number');
	_checkType(b, 'number');
	_checkType(c, 'number');

	const sorted = [a, b, c].sort((x, y) => {
		return compare(x, y);
	});

	[a, b, c] = sorted;

	if (Math.pow(a, 2) + Math.pow(b, 2) == Math.pow(c, 2)) {
		return true;
	} else {
		return false;
	}
}


export {
	factorial, unsafeFactorial,
	compare
}

